/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Session;

import DAO.DAO;
import DTO.Clerk;
import Exception.KwickKioskDBException;
import Utilities.SimpleProtector;

/**
 *
 * @author Spence
 */
public class POS_SessionManager extends SessionManager{

    private Clerk operatingClerk;
    private boolean clerkLoggedIn;

    public POS_SessionManager(DAO dao){
        super(dao);
        clerkLoggedIn = false;
    }

    public boolean isClerkLoggedIn() {
        return clerkLoggedIn;
    }

    public void setClerkLoggedIn(boolean clerkLoggedIn) {
        this.clerkLoggedIn = clerkLoggedIn;
    }

    public Clerk getOperatingClerk() {
        return operatingClerk;
    }

    public boolean clerkLogIn(String clerkId, String password){
        boolean loginSuccess = false;
        String dbPwd = "";
        String decryptedPwd = "";
        try {
            operatingClerk = new Clerk();
            operatingClerk = dao.getClerkInfo(Integer.parseInt(clerkId));
            if(operatingClerk != null){
                dbPwd = operatingClerk.getPassCode();
                decryptedPwd = SimpleProtector.decrypt(dbPwd);
                System.out.println(decryptedPwd);
            }else{
              loginSuccess = false;
            }

        } catch (KwickKioskDBException ex) {
            return false;
        } catch (Exception ex) {
            return false;
            //log.log(Level.SEVERE, null, ex);
        }
        if (password.equals(decryptedPwd)) {
            loginSuccess = true;
            clerkLoggedIn = true;
        }
        else
            loginSuccess = false;

        return loginSuccess;
    }

    public void clerkLogout(){
        operatingClerk = new Clerk();
        clerkLoggedIn = false;
    }
}
